class Sprite_Bar < Sprite
  attr_reader   :_batter_
  attr_reader   :_bar_width
  attr_reader   :_bar_height
  attr_reader   :color_front
  attr_reader   :color_bg
  attr_reader   :type
  attr_reader   :_record_val
  attr_reader   :_bar_current
  attr_reader   :_bar_target
  def initialize
    super
    @_bar_width = 100
    @_bar_height = 25
    @color_front = Color.new(255, 255, 255)
    @color_bg = Color.new(70, 70, 70)
    self.bitmap = Bitmap.new(@_bar_width, @_bar_height)
    self.z = 1000
    self.bitmap.font.color.set(255, 255, 255, 255)
    self.bitmap.font.size = 16
    @_record_val = -1
    @_bar_current = 0
    @_bar_target = 1
  end
  def set_actor_state_bar(type, index, actor)
    @type = type
    @_batter_ = actor
    self.x = index * 160 + 10
    actor_y = 420
    init_current = 0
    init_max = 0
    case type
      when 1
        self.y = actor_y
        @color_front = Color.new(255, 55, 55)
        init_current = actor.hp
        init_max = actor.maxhp
      when 2
        self.y = actor_y + @_bar_height
        @color_front = Color.new(100, 100, 240)
        init_current = actor.sp
        init_max = actor.maxsp
    end
    @_record_val = init_current
    @_bar_target = (@_record_val.to_f / init_max * @_bar_width).round
    @_bar_current = [@_bar_target - 10, -1].max
  end
  def set_energy_bar
    @type = 3
    set_postion(30, 100)
    @color_front = Color.new(183, 255, 57)
    @_record_val = 0
    @_bar_target = 0
    @_bar_current = -1
  end
  def set_enmey_state_bar(enemy)
    @type = 4
    @_batter_ = enemy
    self.x = enemy.screen_x - 30
    self.y = enemy.screen_y
    @_bar_width = 75
    @color_front = Color.new(255, 55, 55)
    @_record_val = enemy.hp
    @_bar_target = ((@_record_val / enemy.maxhp) * @_bar_width).round
    @_bar_current = [@_bar_target - 10, -1].max
    self.z = 10
    self.bitmap.dispose
    self.bitmap = Bitmap.new(@_bar_width, @_bar_height)
    self.bitmap.font.size = 12
  end
  def set_postion(x, y)
    self.x = x
    self.y = y
  end
  def refresh(current, max)
    if @_record_val != current
      @_record_val = current
      @_bar_target = (@_record_val.to_f / max * @_bar_width).round
    end
    if @_bar_current == @_bar_target
      return
    end
    if @_bar_target > @_bar_current
      @_bar_current = [(@_bar_current += 1), @_bar_target].min
    end
    if @_bar_target < @_bar_current
      @_bar_current = [(@_bar_current -= 1), @_bar_target].max
    end
    bitmap = self.bitmap
    bitmap.clear
    # fill_rect(x, y, width, height, color)
    case @type
      when 1,2
        bitmap.draw_text(0, 0, @_bar_width, 13, @_record_val.to_s)
        bitmap.fill_rect(0, 13, @_bar_width, 10, @color_bg)
        bitmap.fill_rect(0, 14, @_bar_current, 8, @color_front)
      when 3
        bitmap.draw_text(0, 0, @_bar_width, 13, "技能点 " + @_record_val.to_s)
        bitmap.fill_rect(0, 13, @_bar_width, 10, @color_bg)
        bitmap.fill_rect(0, 14, @_bar_current, 8, @color_front)
      when 4
        bitmap.draw_text(0, 0, @_bar_width, 10, _batter_.name)
        bitmap.fill_rect(0, 10, @_bar_width, 7, @color_bg)
        bitmap.fill_rect(0, 11, @_bar_current, 5, @color_front)
    end
  end
end
